/**
 * @author 王晓伟
 * 部门树
 */
var  departmentstore,departmentwin,departmenttempstore;
var  departmentid="",departmentname="", departmentidtager="",departmentnametager="";

function initdepartmenttree(){
	
	 Ext.BLANK_IMAGE_URL=Ext.get('basePath').getValue()+"ajaxFramework/ext-2.1/resources/images/default/s.gif";
	 var url = Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/departmenttree.do';
	 var load = new Ext.tree.TreeLoader({
	 	dataUrl:url
	 });
	 var root = new Ext.tree.AsyncTreeNode({
	 	id:'0',
	 	text:'天津利达粮油有限公司'
	 })
	  
	 var tree = new Ext.tree.TreePanel({
	 		x:0,
	 		y:0,	     
        	autoScroll: true,
        	animate: true,
        	containerScroll: true,
        	border: true,
	        loader:load,
	        height:420,
	        width:230,
	        anchor:'0% 100%' 
	        
	    });
	tree.setRootNode(root); 
	tree.on('dblclick',function(node){
			
			if(node.id!='0'&&departmentstore.find('deptId',node.id) ==-1)
			{
				var orderNo = 1;
				if(departmentstore.getCount()>0)
				{
					orderNo	= departmentstore.getAt(departmentstore.getCount() - 1).get('orderNo')+1;
				}
				departmentstore.add(new Ext.data.Record({
				orderNo:orderNo,
				deptName:node.text,
				id:0,
				deptId:node.id
			}));	
		}		
	});
	
	
	/**
	 * 生成表格
	 */
	 var departmentstoreurl = Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/selectuserdepartment.do?userid='+Ext.get('userid').getValue();
	 departmentstore = new Ext.data.JsonStore({
		url:departmentstoreurl,
		fields:['orderNo','deptName','id','deptId','operate']
	});
	departmenttempstore = new Ext.data.JsonStore({
		url:departmentstoreurl,
		fields:['orderNo','deptName','id','deptId','operate']
	});
	departmentstore.load();
	departmenttempstore.load();
	var cm = new Ext.grid.ColumnModel([
		{header: "序号", width: 25, dataIndex: 'orderNo',menuDisabled : true},
		{header: "部门名称", width: 75, dataIndex: 'deptName',menuDisabled : true},
		{header: "操作", width: 75, dataIndex: 'operate',menuDisabled : true,renderer:function(s,m,rec){
		return "<a  href=javascript:void(0) onclick='javascript:departmentremove("+rec.data.deptId+" )'>移除</a>";
		}},
		{header: "id", width: 75, dataIndex: 'id',menuDisabled : true,hidden:true},
		{header: "部门ID", width: 75, dataIndex: 'deptId',menuDisabled : true,hidden:true}
	]);
	var grid = new Ext.grid.GridPanel({
		x:tree.width,
	 	y:0,
		cm:cm,
		width:270,
		height:420,
		autoScroll:true,
		anchor:'100% 100%',
		store:departmentstore,
		border: true,
	   	viewConfig:{
			forceFit:true	
		}
	});
    departmentwin = new Ext.Window({
		layout:'absolute',
		title:'部门选择',
		width:520,
		height:500,
		modal:true,
		closeAction:'hide',
		items:[tree,grid],
		buttonAlign:'right',
		buttons:[{
			text:'保存',
			handler:function(){
				departmentid='';
				departmentname='';
				departmentstore.each(function(record){
				departmentid+=record.get('deptId')+",";
				departmentname+=record.get('deptName')+",";
				$("#"+departmentidtager).attr('value',departmentid);
				$("#"+departmentnametager).attr('value',departmentname);
				
				});
				$("#"+departmentidtager).attr('value',departmentid);
				$("#"+departmentnametager).attr('value',departmentname);
				departmenttempstore.removeAll();
				departmentstore.each(function(record){
					departmenttempstore.addSorted(record);	
				});
				departmentwin.hide();
			}
		},{
			text:'取消',
			handler:function(){
				departmentstore.removeAll();
				departmenttempstore.each(function(record){
					departmentstore.addSorted(record);	
				});
				departmentwin.hide();		
			}
		}]
		
	})


}
/**
 * 移除表格记录
 */
function departmentremove(deptId){
	departmentstore.remove(departmentstore.getAt(departmentstore.find('deptId',deptId)));
	
}

function departmentwinshow(id,name){
	departmentidtager = id;
	departmentnametager = name;
	departmentwin.show(Ext.getBody());
	departmentwin.findByType('treepanel')[0].root.expand();  
}